<?php
namespace Api\Controller;
use Think\Controller;

class OldController extends BaseController {

   private $m = NULL;
   private $md = NULL;
   private $navName = '订单管理';


   public function _initialize(){
   		parent::_initialize();
		$this->m = M('customer_oldthing');
		
		
		
   }
    	 	
	/**   聚餐美食-猜你喜欢  liuq
	*
	*/	
  
   public  function slist(){
     $page  = I('page');//页码
       //$cid = I('cid');//  用户标识  member id
     $data = $this->m->where()->order('add_time desc')->page($page.',10')->select();      
     $count_num = $this->m->where()->order('add_time desc')->count();
       foreach( $data as $key => $item){
      
           $data1[$key]['id'] = $item['id']; 
             $data1[$key]['title'] = $item['title'];
             //  $data1[$key]['intro'] = $item['require'];
                $data1[$key]['price'] = $item['price'];
                  $data1[$key]['add_time'] = $item['add_time'];
                    $data1[$key]['nickname'] = M('customer')->where('cid="'.$item['cid'].'"')->getField('nickname');  
                      
                  $old_pics = $item['pics']; //evaluate_pics
              //    $old_pics = substr($old_pics, 0, -1);
                  $datapics = explode("|", $old_pics);
                  $data[$key]["pics"]=$datapics;
                  foreach ( $data[$key]["pics"] as $key1 => $value) {
                    $data1[$key]["pics"][$key1]=__VUPLOAD__.'old_pics/'.$value;
                  }
                  
                      
          }
 //   $data2['count_num'] =  $count_num;
 
    returnApiData3('0','成功',$data1, $data2,$data3,$count_num);exit;	     
       
   }
   
  
   
   public function choice_type(){
       $page  = I('page');//页码
       $choice_type = I('choice_type'); //旧物分类 1电子产品   2 衣物  3书籍笔记 4车辆 5 宠物用品 6其他
       $data = $this->m->where('type="'.$choice_type.'"')->order('add_time  desc')->page($page.',10')->select();
       $count_num = $this->m->where('type="'.$choice_type.'"')->order('add_time  desc')->count();
      
       foreach( $data as $key => $item  ){
             $data1[$key]['id'] = $item['id']; 
                $data1[$key]['title'] = $item['title'];
                 // $data1[$key]['intro'] = $item['require'];
                    $data1[$key]['price'] = $item['price'];
                      $data1[$key]['add_time'] = $item['add_time'];
                         $data1[$key]['nickname'] = M('customer')->where('cid="'.$item['cid'].'"')->getField('nickname');  
                      
                  
                  
                  $old_pics = $item['pics']; //evaluate_pics
                  $datapics = explode("|", $old_pics);
                  $data[$key]["pics"]=$datapics;
                  foreach ( $data[$key]["pics"] as $key1 => $value) {
                    $data1[$key]["pics"][$key1]=__VUPLOAD__.'old_pics/'.$value;
                  }
           
       }
       
        returnApiData3('0','成功',$data1, $data2,$data3,$count_num);exit;	   
       
   }


   
     
   public function choice_time(){
       $page  = I('page');//页码
       $add_time = strtotime(I('add_time'));
       $data = $this->m->where('add_time="'.$add_time.'"')->order('add_time  desc')->page($page.',10')->select();
       $count_num = $this->m->where('add_time = "'.$add_time.'"')->order('add_time  desc')->count();
      
       foreach( $data as $key => $item  ){
             $data1[$key]['id'] = $item['id']; 
                $data1[$key]['title'] = $item['title'];
                 // $data1[$key]['intro'] = $item['require'];
                    $data1[$key]['price'] = $item['price'];
                      $data1[$key]['add_time'] = $item['add_time'];
                         $data1[$key]['nickname'] = M('customer')->where('cid="'.$item['cid'].'"')->getField('nickname');  
                      
                  
                  
                  $old_pics = $item['pics']; //evaluate_pics
                  $datapics = explode("|", $old_pics);
                  $data[$key]["pics"]=$datapics;
                  foreach ( $data[$key]["pics"] as $key1 => $value) {
                    $data1[$key]["pics"][$key1]=__VUPLOAD__.'old_pics/'.$value;
                  }
           
       }
       
       returnApiData3('0','成功',$data1, $data2,$data3,$count_num);exit;	   
       
   }

   public function old_detail(){
    //   echo(strtotime("2017-07-04 15:00:00"));exit;
      $id  =  I('id');
      $dataInfo = $this->m->where('id="'.$id.'"')->find();
      $data1['title'] = $dataInfo['title'];
      $data1['nickname'] =  M('customer')->where('cid="'.$dataInfo['cid'].'"')->getField('nickname');
      $data1['price'] = $dataInfo['price'];
      $data1['intro'] = $dataInfo['intro'];
      $data1['cus_heapic'] = M('customer')->where('cid="'.$dataInfo['cid'].'"')->getField('cus_heapic')?__VUPLOAD__.'heapic/'.M('customer')->where('cid="'.$dataInfo['cid'].'"')->getField('cus_heapic'):"";
      $the_time  = $dataInfo['add_time'];
      $return_time = $this->time_tran($the_time);
      $data1['show_time'] =   $return_time;
      
      $old_pics = $dataInfo['pics'];
      //$old_pics = substr($old_pics, 0, -1);
      $datapics = explode("|", $old_pics);
      $data["pics"]=$datapics;    
      foreach ( $data["pics"] as $key1 => $value) {
              $data1["pics"][$key1]=__VUPLOAD__.'old_pics/'.$value;
      }
    
      returnApiData2('0','成功',$data1, $data2,$data3);exit;
   }
   
   
   
   
   function time_tran($the_time) {  
    $now_time = date("Y-m-d H:i:s", time());  
  
    $now_time = strtotime($now_time); 
    
    $show_time = $the_time;  
    $dur = $now_time - $show_time;  
     
    if ($dur < 0) {  
        return $the_time;  
    } else {  
        if ($dur < 60) {  
            return $dur . '秒前';  
        } else {  
            if ($dur < 3600) {  
                return floor($dur / 60) . '分钟前';  
            } else {  
                if ($dur < 86400) {  
                    return floor($dur / 3600) . '小时前';  
                } else {  
                    if ($dur < 259200) {//3天内  
                        return floor($dur / 86400) . '天前';  
                    } else {  
                       // return $the_time;
                        return '超过3天前';
                    }  
                }  
            }  
        }  
    }  
}  
   
   
   public  function  my_old_slist(){
        $page  = I('page');//页码
        $cid = I('cid');
        $data = $this->m->where('cid="'.$cid.'"')->order('add_time  desc')->page($page.',10')->select();
        $count_num = $this->m->where('cid="'.$cid.'"')->order('add_time desc')->count();
        
        foreach( $data as $key => $item){
      
           $data1[$key]['id'] = $item['id']; 
             $data1[$key]['title'] = $item['title'];
             //  $data1[$key]['intro'] = $item['require'];
                $data1[$key]['price'] = $item['price'];
                 $data1[$key]['add_time'] = $item['add_time'];
                  $data1[$key]['nickname'] = M('customer')->where('cid="'.$item['cid'].'"')->getField('nickname');  
                      
                  $old_pics = $item['pics']; //evaluate_pics
                  $datapics = explode("|", $old_pics);
                  $data[$key]["pics"]=$datapics;
                  foreach ( $data[$key]["pics"] as $key1 => $value) {
                    $data1[$key]["pics"][$key1]=__VUPLOAD__.'old_pics/'.$value;
                  }
                  
                     
          }
 
     returnApiData3('0','成功',$data1, $data2,$data3,$count_num);exit;	    
   }
   
   
   
   
   public function add_old(){
       $info['type'] = I('type');  //旧物分类 1电子产品   2 衣物  3书籍笔记 4车辆 5 宠物用品 6其他
       $info['title'] = I('title');
       $info['price'] = I('price');
       $info['intro'] = I('intro');
       $info['cid'] = 22;
                 
     ///////处理上传的多图///////////
      
       $old_pics   = I('old_pics');
       
      // dump($old_pics);exit;
       $fp = fopen('aa.txt','w+'); 
       fwrite($fp,var_export($old_pics,true)); 
       fclose($fp);
       
     
      // $info['pics'] = $this->allImg($old_pics, 'Uploads/old_pics', $sign, $day);
       
       $info['pics'] = $this->sendpic($old_pics, 'Uploads/old_pics/', $sign, $day);
       ///////处理上传的多图///////////
       $result = $this->m->add($info);
       if( $result ){
           returnApiresult("0", "成功");exit;
        }
   }
   
   
   //多图上传
 function sendpic($img,$address){
     
     if(!is_array($img)){
       $img=explode("|", $img);
       }
       for($i=0;$i<count($img);$i++){
           $newImgType= substr($img[$i],0,strrpos($img[$i],';'));  //图片的类型
           $base=substr($img[$i],strrpos($img[$i],';')+1);
           $a=explode(',',$base);
           $types=explode('/',$newImgType);
           $type=$types[1];
       //    dump($type);exit;
           $filename = rand(0,100000).time().".".$type; 
           $xmlstr=base64_decode($a[1]);
           makeDir($address);
           $file = fopen($address.$filename,"w");//打开文件准备写入  
           fwrite($file,$xmlstr);//写入
           fclose($file);//关闭
           $fileaddress[]=$filename;
//           $fileaddress[]=$filename;
       } 
       $info=implode('|',$fileaddress);
       //return $info.'|';
       return $info;
    }

   
//手机多张图片上传
function allImg($s_imgphoto,$address,$sign,$day){
       if(!is_array($s_imgphoto)){
       $s_imgphoto=explode("|", $s_imgphoto);
       }
       $address=ltrim($address,'/');
     foreach ($s_imgphoto as $key => $value) {
            $newImgType[$key] = substr($value,0,strrpos($value,','));  //图片的类型
            $post=strrpos($value,',')+1;
            $newImgbase64[$key] = substr($value,$post);  
            $filename = rand(0,100000).time().".".$newImgType[$key]; //图片二进制流
            $xmlstr=base64_decode($newImgbase64[$key]);
            $jpg = $xmlstr; //得到post过来的二进制原始数据
             if($day==1){
               $y=date("Y",time());
               $m=date("m",time());
               $d=date("d",time());
               $day=$y.'-'.$m.'-'.$d;
               $address=$address.$day.'/';
                       }
            makeDir($address);
            $file = fopen($address.$filename,"w");//打开文件准备写入  
            $fileaddressS_imgphoto[$key]=$filename.'|';
            fwrite($file,$jpg);//写入
            fclose($file);//关闭
        } 
        foreach ($fileaddressS_imgphoto as $key => $value) {
          $s_imgphotoUrlAll.=$value;  //多张图片拼接成一个字符串
        }
        
        $s_imgphotoUrlAll=substr($s_imgphotoUrlAll, 0, -1);
        $s_imgphotoUrlAll=ltrim($s_imgphotoUrlAll,'/');
         // $_POST['s_imgphoto']=$s_imgphotoUrlAll;
        return '/'.$s_imgphotoUrlAll;
}
   
   


   
   
      
   public  function  add_jobwant(){
      $info['cid'] =  I('cid');
      $info['title'] =  I('tilte');
      $info['require']  = I('require');
      $info['intro'] = I('intro');
      $info['add_time'] = I('add_time');
       //图片
      $result =  $this->m->add($info);
      if( $result ){
          
            returnApiresult("0", "成功");exit;
          
      }
       
   } 
   
 public  function  edit_jobwant(){
     
      $id  = I('id');
      $dataInfo = $this->m->where('id="'.$id.'"')->find();
      $data['id'] = $dataInfo['id']; // 下一个  编辑之后的 保存的接口用到
      $data['title'] = $dataInfo['title'];
      $data['rqueire'] = $dataInfo['require'];
      $data['intro'] = $dataInfo['intro'];
      
      returnApiData2('0','成功',$data, $data2,$data3);exit;
     
 } 
           
           
 public function   save_jobwant(){
    $id  = I('id');
    $info['title'] = I('title');
    $info['rqueire'] = I('rqueire'); 
    $info['intro'] = I('info');
    $result = $this->m->where('id="'.$id.'"')->save($info);
    if(  $result ){
        
          returnApiresult("0", "修改成功");exit;
          
    }
    
 }
   
   
   
 public  function  del_jobwant(){
    $id = I('id');
    $result = $this->m->where('id="'.$id.'"')->delete(); 
        if(  $result ){
             returnApiData("0","删除成功");exit;

         }
 }
     

}